<?php
/**
 * 建造者模式
 * This file is part of pattern.
 * Author: tangzhou
 * Date: 2016/8/29
 * Time: 11:07
 * File: Builder.php
 */

//1.产品角色，产品角色定义自身的组成属性
//2.抽象建造者，抽象建造者定义了产品的创建过程以及如何返回一个产品
//3.具体建造者，具体建造者实现了抽象建造者创建产品过程的方法，给产品的具体属性进行赋值定义
//4.指挥者，指挥者负责与调用客户端交互，决定创建什么样的产品

/**
 * 具体产品角色
 * Class Bird
 */
class Bird
{
    public $header;
    public $wing;
    public $foot;

    public function show()
    {
        echo "头的颜色:{$this->header}<br/>";
        echo "翅膀的颜色:{$this->wing}<br/>";
        echo "脚的颜色:{$this->foot}<br/>";
    }
}

/**
 * 抽象建造者
 * Class BirdBuilder
 */
abstract class BirdBuilder
{
    protected $bird;

    function __construct()
    {
        $this->bird = new Bird();
    }

    abstract function BuildHeader();
    abstract function BuildWing();
    abstract function BuildFoot();
    abstract function GetBird();
}

/**
 * 具体建造者
 * Class BuleBird
 */
class BuleBird extends BirdBuilder
{

    function BuildHeader()
    {
        $this->bird->header='bule';
    }

    function BuildWing()
    {
        $this->bird->wing='bule';
    }

    function BuildFoot()
    {
        $this->bird->foot='bule';
    }

    function GetBird()
    {
        return $this->bird;
    }
}

/**
 * 红色
 * Class RedBird
 */
class RedBird extends BirdBuilder
{
    function BuildHeader()
    {
        $this->bird->header='red';
    }

    function BuildWing()
    {
        $this->bird->wing='red';
    }

    function BuildFoot()
    {
        $this->bird->foot='red';
    }

    function GetBird()
    {
        return $this->bird;
    }
}

/**
 * 指挥者
 * Class Direcotr
 */
class Direcotr
{
    /**
     * @param BirdBuilder $builder
     * @return Bird
     */
    function build(BirdBuilder $builder)
    {
        $builder->BuildHeader();
        $builder->BuildWing();
        $builder->BuildFoot();

        return $builder->GetBird();
    }
}

$director = new Direcotr();

$blue = $director->build(new BuleBird());
$blue->show();
